$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL +'sys/pushversion/list',
        datatype: "json",
        colModel: [			
			{ label: 'rid', name: 'rid', index: 'rid', width: 50, key: true },
			{ label: '所属项目', name: 'project', index: 'project', width: 80 }, 			
			{ label: '发布版本号', name: 'version', index: 'version', width: 80 },
			{ label: '发布环境', name: 'environment', index: 'environment', width: 80 },
			{ label: '发布时间', name: 'createTm', index: 'create_tm', width: 80 },
            { label: '发布人', name: 'createUser', index: 'create_user', width: 80 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        	// vm.dicList();
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			tableName: null
		},
        rules: {
            name: {
                required: true,
                minlength: 4
            },
            code: {
                required: true,
                minlength: 4
            },
            address: {
                required: true,
                minlength: 4
            },
            createTime: {
                required: true,
                minlength: 2
            },
            phone: {
                required:true,
                isPhone: true
            }
        },
		title: null,
		pushVersion: {},
        dicList:{},
        projectDicList:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            $("#option_list").modal('show');
			vm.title = "新增";
			vm.pushVersion = {project:"宁家鲜生",environment:"test"};
            vm.dicList();
		},
		update: function (event) {
			var rid = getSelectedRow();
			if(rid == null){
				return ;
			}
            $("#option_list").modal('show');
            vm.title = "修改";
            
            vm.getInfo(rid)
            vm.dicList();

		},
		saveOrUpdate: function (event) {
			var url = vm.pushVersion.rid == null ? baseURL +"sys/pushversion/save" : baseURL +"sys/pushversion/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.pushVersion),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var rids = getSelectedRows();
			if(rids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL +"sys/pushversion/delete",
				    contentType: "application/json",
				    data: JSON.stringify(rids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(rid){
			$.get(baseURL +"sys/pushversion/info/"+rid, function(r){
                vm.pushVersion = r.pushVersion;
            });
		},
		reload: function (event) {
            //$("#option_list").modal('hide');
            // vm.showList = true;
            $("#option_list").modal('hide');
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                //postData:{'roleName': vm.q.roleName},
                page:page
            }).trigger("reloadGrid");
		},
        getDicList: function(){
            $.get(baseURL + "sys/dic/select?code=MODULE", function(r){
                vm.dicList = r.list;
            });
        },
        getProjectDicList: function(){
            $.get(baseURL + "sys/dic/select?code=PROJECT", function(r){
                vm.projectDicList = r.list;
            });
        },

        changeType: function (ele) {
            var optionTxt = $(ele.target).find("option:selected").text();
            var optionVal = ele.target.value;
        }
    },
    mounted: function () { this.getProjectDicList(); }
});